﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GestorDeFlotasDesktop.AbmAuto
{
    public partial class Listado_Auto : Form
    {
        private DataTable dt;
        private Modelo.FormActions.Actions action;

        public Listado_Auto(Modelo.FormActions.Actions runAction)
        {
            action = runAction;
            InitializeComponent();

            SqlConnection con = Modelo.Modelo.createConnection();
            SqlDataAdapter da = new SqlDataAdapter(Modelo.SqlQueries.getMarcasTaxi(), con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            busqueda_marca_cb.DataSource = ds.Tables[0].DefaultView;
            busqueda_marca_cb.DisplayMember = "Marca";
            busqueda_marca_cb.ValueMember = "Id";
            busqueda_marca_cb.SelectedIndex = -1;
            Modelo.Modelo.closeConnection(con);

            SqlConnection con2 = Modelo.Modelo.createConnection();
            SqlDataAdapter da2 = new SqlDataAdapter(Modelo.SqlQueries.getMarcaModeloReloj(), con2);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2);
            busqueda_reloj_cb.DataSource = ds2.Tables[0].DefaultView;
            busqueda_reloj_cb.DisplayMember = "Marca";
            busqueda_reloj_cb.ValueMember = "Id";
            busqueda_reloj_cb.SelectedIndex = -1;
            Modelo.Modelo.closeConnection(con2);

            FillData();
        }

        void FillData()
        {
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                SqlDataAdapter da;
                dt = new DataTable();
                switch (action)
                {
                    case Modelo.FormActions.Actions.Baja:
                        da = new SqlDataAdapter(Modelo.SqlQueries.getEnabledAutos, con);
                        da.Fill(dt);
                        break;
                    case Modelo.FormActions.Actions.Modificacion:
                        da = new SqlDataAdapter(Modelo.SqlQueries.getAutos(), con);
                        da.Fill(dt);
                        break;
                }
                dataGridView1.DataSource = dt;
            }

        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            int columnIndex = e.ColumnIndex;
            DataGridViewRow row = dataGridView1.Rows[rowIndex];
            Login.Login.mainForm.openForm(new AbmAuto(action, Convert.ToInt32(row.Cells["Id"].Value)));
        }

        private void buscar_btn_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                
                SqlDataAdapter da = new SqlDataAdapter(Modelo.SqlQueries.getAutoFiltered(
                        busqueda_marca_cb.SelectedValue == null ? -1 : Convert.ToInt32(busqueda_marca_cb.SelectedValue),
                        busqueda_modelo_tb.Text,
                        busqueda_patente_tb.Text,
                        busqueda_licencia_tb.Text,
                        busqueda_reloj_cb.SelectedValue == null ? -1 : Convert.ToInt32(busqueda_reloj_cb.SelectedValue)), con);
                dt.Dispose();
                dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

        private void limpiar_btn_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                SqlDataAdapter da = new SqlDataAdapter(Modelo.SqlQueries.getAutos(), con);
                dt.Dispose();
                dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }
    }
}
